import http from "@/api";
import { ReqLogin, ResLogin } from "@/api/interface/index";

export const loginApi = (params: ReqLogin, headers = {}) => {
  const formData = new FormData();
  Object.entries({
    client_id: "client",
    client_secret: "secret",
    grant_type: "password",
    scope: "all",
    ...params
  }).forEach(([k, v]) => {
    formData.set(k, v);
  });
  return http.post<ResLogin>(`/oauth/token`, formData, {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      ...headers
    }
  });
};

export const mockLogin = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({ access_token: "dshjfs9ja719381-123" });
    }, 1000);
  });
};

export function fetchLogout(data = {}) {
  return http.post("/current/user/log/out", data);
}
